

//
//  XYCallDetailFooterView.m
//  Zhihuijinfu
//
//  Created by XY Lv on 17/3/28.
//  Copyright © 2017年 王笛. All rights reserved.
//

#import "XYCallDetailFooterView.h"
#import "XYPhotosCollectionViewCell.h"

@implementation XYCallDetailFooterView
{
    CGFloat _xyItemW;
    CGFloat _xyItemH;
    CGFloat _xyTitleLabelH;
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self xyInitSubviews];
        [self xySetSubviewsFrame];
    }
    return self;
}

- (void)xyInitSubviews{
    _xyTitleLabel = [[UILabel alloc]init];
    [self addSubview:_xyTitleLabel];
    _xyTitleLabel.text = @"相关照片";
    _xyTitleLabel.textColor = [UIColor colorWithRed:0.22 green:0.22 blue:0.22 alpha:1.00];
}

- (void)xySetSubviewsFrame{
    CGFloat xyTitleLabelX  = xyPadding;
    CGFloat xyTitleLabelY  = xyPadding;
    CGFloat xyTitleLabelW  = xy(60);;
           _xyTitleLabelH  = xy(25);
    
    _xyTitleLabel.frame = CGRectMake(xyTitleLabelX, xyTitleLabelY, xyTitleLabelW, _xyTitleLabelH);
    
    UICollectionViewFlowLayout * xyLayout = [[UICollectionViewFlowLayout alloc]init];
    xyLayout.itemSize = CGSizeMake(xy(30), xy(30));
    xyLayout.minimumLineSpacing = xyPadding *2;
    xyLayout.minimumInteritemSpacing = xyPadding *2;
    
    [xyLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    _xyPhotosCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(_xyTitleLabel.frame), WDScreenW ,CGRectGetHeight(self.frame) - _xyTitleLabelH) collectionViewLayout:xyLayout];

    // 注册Cell
    _xyPhotosCollectionView.backgroundColor = [UIColor whiteColor];
    [_xyPhotosCollectionView registerClass:[XYPhotosCollectionViewCell class] forCellWithReuseIdentifier:@"xyCell"];
    
    [self addSubview:_xyPhotosCollectionView];
    _xyPhotosCollectionView.dataSource = self;
    _xyPhotosCollectionView.delegate = self;
    
    _xyItemW = xy(130);
    _xyItemH = 100*_xyItemW/135.00f;
    
    
    
}

#pragma mark - <UICollectionView代理>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return _xyPhotosMArr.count/2+_xyPhotosMArr.count %2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    XYPhotosCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"xyCell" forIndexPath:indexPath];
//    cell.xyDelegate = self;
    NSInteger  num = indexPath.section*2+indexPath.row;
    cell.tag = num;
    if(_xyPhotosMArr.count >num){
        NSDictionary * dic = [_xyPhotosMArr objectAtIndex:num];
        [cell.xyImgView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"url"]] placeholderImage:[UIImage imageNamed:@""]];
    }
    
    

    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(_xyItemW, _xyItemH);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    UICollectionViewFlowLayout *flowLayout =
    (UICollectionViewFlowLayout *)collectionViewLayout;
    flowLayout.minimumInteritemSpacing = xyPadding *2;
    flowLayout.minimumLineSpacing = xyPadding *2;
    
    return UIEdgeInsetsMake(0,xyPadding , xyPadding *2, xyPadding);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if(self.delegate&&[self.delegate respondsToSelector:@selector(xyPhotoTapToBrowserWithIndexPath:)]){
        [self.delegate xyPhotoTapToBrowserWithIndexPath:indexPath];
    }
    
}


- (void)setXyPhotosMArr:(NSMutableArray *)xyPhotosMArr{
    _xyPhotosMArr = xyPhotosMArr;
    self.xyFooterH = (xyPadding *2+_xyItemH)*(_xyPhotosMArr.count/2+_xyPhotosMArr.count %2)+_xyTitleLabelH;
    [self xyRefreshFrame];
    [_xyPhotosCollectionView reloadData];
}

- (void)xyRefreshFrame{
    self.frame = CGRectMake(0, 0, WDScreenW,self.xyFooterH);
    _xyPhotosCollectionView.frame = CGRectMake(0, CGRectGetMaxY(_xyTitleLabel.frame), WDScreenW ,CGRectGetHeight(self.frame) - _xyTitleLabelH);

}

@end
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
